package cn.bincker.mycaptcha.basic.entity;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.lang.reflect.InvocationTargetException;

@Getter
@Setter
@AllArgsConstructor
public class MyQuestion<C extends MyCaptcha, A extends MyAnswer> implements Cloneable {
    private C captcha;
    private A answer;

    @SuppressWarnings("unchecked")
    @Override
    public MyQuestion<C, A> clone() {
        try {
            MyQuestion<C, A> clone = (MyQuestion<C, A>) super.clone();
            if(captcha instanceof Cloneable){
                clone.captcha = (C) captcha.getClass().getMethod("clone").invoke(captcha);
            }
            if(answer instanceof Cloneable){
                clone.answer = (A) answer.getClass().getMethod("clone").invoke(answer);
            }
            return clone;
        } catch (CloneNotSupportedException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            throw new AssertionError();
        }
    }
}
